package edu.berkeley.cs160.dereknelson.prog2;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class Painter extends Activity {
	private final int clearId = 1;
	private final int settingsId = 2;
	private final int optionsId = 0;
	
	private PainterCanvas pc = null;
	
    public boolean onCreateOptionsMenu(Menu m) {
        m.add(optionsId, clearId, 0, "Clear");
        m.add(optionsId, settingsId, 1, "Settings");
        return super.onCreateOptionsMenu(m);
    }
    
    private void openSettings() {
    	Intent intent = new Intent(this, edu.berkeley.cs160.dereknelson.prog2.PainterSettings.class);
    	startActivityForResult(intent, 1);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// Since we only have one other activity, we can assume it's the settings activity calling
    	Bundle settings = data.getExtras();
    	String mode = settings.getString("mode");
    	if (mode.equals("erase")) {
    		// Just overwrite with white paint to erase
    		pc.setBrushColor(Color.WHITE);
    	} else {
    		String color = settings.getString("color");
    		if (color.equals("gray")) {
    			pc.setBrushColor(Color.GRAY);
    		} else if (color.equals("blue")) {
    			pc.setBrushColor(Color.BLUE);
    		} else if (color.equals("red")) {
    			pc.setBrushColor(Color.RED);
    		}
    	}
    	int size = Integer.parseInt(settings.getString("size"));
    	pc.setBrushSize(size);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case clearId:
        	// Clear the drawing
        	pc.clear();
            return true;
        case settingsId:
        	openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pc = new PainterCanvas(this);
        setContentView(pc);
    }
}